// pages/index/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgs:['../../images/banner0.png','../../images/banner1.png'],
    icons: [{
        img: '/images/icon1.png',
        txt: '院校'
      },
      {
        img: '/images/icon9.png',
        txt: '证书'
      },
      {
        img: '/images/icon2.png',
        txt: '专业'
      },
      {
        img: '/images/icon3.png',
        txt: '历年数据'
      },
    ],
    icons1: [{
        img: '/images/icon4.png',
        txt: '专插本'
      },
      {
        img: '/images/icon5.png',
        txt: '热度排名'
      },
      {
        img: '/images/icon6.png',
        txt: '地区微群'
      },
      {
        img: '/images/icon7.png',
        txt: '教材课程'
      },
    ],
    nodata: true,
    schools: [],
    news: [],
    jumpUrl: '',
    swiperCurrent: 0,
  },
  swiperChange: function (e) {
    // if (e.detail.source == "touch") {      
    //   //防止swiper控件卡死
    //   if (this.data.swiperCurrent == 0 && this.data.preIndex>1 ) {//卡死时，重置current为正确索引
    //     this.setData({ swiperCurrent: this.data.preIndex });
    //   }
    //   else {//正常轮转时，记录正确页码索引
    //     this.setData({ swiperCurrent: this.data.swiperCurrent });
    //   }
    // }

    this.setData({
      swiperCurrent: e.detail.current
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this;
    // console.log('app.globalData', app.globalData.user);
    // console.log('request');
    wx.login({
      success(res1){
        // console.log('res1',res1); 
      },fail(err){
        console.log(err);
      },complete(con){
        console.log(con);
      }
    })
    
    app.globalData.wxRequest.requestGet('major/getPopularMajor.do', {}).then(res => {
      // console.log(res);
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
        return
      }
      if(res.code == 200) {
        that.setData({
          hotmajor: res.data,
        })
      }
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
    })

    app.globalData.wxRequest.requestGet('data/getInformation.do', {
      pageSize: 6
    }).then(res => {
      // console.log(res);
      if (res.code == '201') {
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
      let news = res.data;
      that.setData({
        news: news,
      })
    }).catch(err => {
      console.log(err);
      wx.showToast({
        title: '请求失败',
        icon: 'error'
      })
    })

    wx.login({
      success: res => {
        // console.log('login',res);
        app.globalData.wxRequest.request('user/login.do', {
          code: res.code
        }).then(res1 => {
          if (res1.code == '201') {
            wx.showToast({
              title: res.msg,
              icon: 'error'
            })
          }
          // console.log('login_code',res1);
          app.globalData.openId = res1.data.openId
          app.globalData.isSignUp = res1.data.isSignUp
          app.globalData.user = res1.data
          let openId = app.globalData.openId
          // console.log('openId', app.globalData.openId);
          if (!openId) {
            return
          }
          app.globalData.wxRequest.requestGet('volunteer/getUserDetail.do', {
            openId: openId
          }).then(res2 => {
            if (res2.code == '201') {
              wx.showToast({
                title: res2.msg,
                icon: 'error'
              })
            }
            if(res2.code == 200){
              app.globalData.wxRequest.requestGet('volunteer/getRecommendedColleges.do', {
                id: app.globalData.user.id
              }).then(res => {
                // console.log(res);
                if (res.code == '201') {
                  wx.showToast({
                    title: res.msg,
                    icon: 'error'
                  })
                }
                that.setData({
                  schools: res.data,
                })
              }).catch(err => {
                console.log(err);
                wx.showToast({
                  title: '请求失败',
                  icon: 'error'
                })
              })
            }
            that.setData({
              userInfo: res2.data,
            }, function () {
              that.setData({
                nodata: false
              })
            })
          }).catch(err => {
            wx.showToast({
              title: '请求失败',
              icon: 'error'
            })
          })
          
        }).catch(err => {
          wx.showToast({
            title: '请求失败',
            icon: 'error'
          })
        })
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  navToNews(e) {
    let jumpUrl = e.currentTarget.dataset.url
    app.globalData.jumpUrl = jumpUrl
    // console.log('jumpUrl', jumpUrl);
    wx.navigateTo({
      url: '/pages/newslists/newsDetail/newsDetail',
    })
  },
  editUserInfo(e) {
    wx.navigateTo({
      url: '/pages/fill/fillform/fillform?edit=1',
    })
  },
  navTo() {
    wx.navigateTo({
      url: '/pages/fill/fill',
    })
  },
  navToSearch() {
    wx.navigateTo({
      url: '/pages/search/search'
    })
  },
  naToAddInfo() {
    wx.navigateTo({
      url: '/pages/fill/fillform/fillform',
    })
  },
  clickIcon1(e) {
    let index = e.currentTarget.dataset.index;
    if (index == 0) {
      wx.navigateTo({
        url: '/pages/school/school',
      })
    }
    if (index == 1) {
      wx.navigateTo({
        url: '/pages/certificate/certificate',
      })
    }
    if (index == 2) {
      wx.navigateTo({
        url: '/pages/major/major',
      })
    }
    if (index == 3) {
      wx.navigateTo({
        url: '/pages/history/history',
      })
    }


  },
  clickIcon2(e) {
    let index = e.currentTarget.dataset.index;
    if (index == 0) {
      // wx.showToast({
      //   title: '暂未开放',
      //   icon: 'error'
      // })
      wx.navigateTo({
        url: '/pages/majorben/majorben',
      })
    }
    if (index == 1) {
      wx.navigateTo({
        url: '/pages/hotrank/hotrank',
      })
    }
    if (index == 2) {
      wx.navigateTo({
        url: '/pages/chatgroup/chatgroup',
      })
    }
    // if (index == 3) {
    //   wx.showToast({
    //     title: '暂未开放',
    //     icon: 'error'
    //   })
    // }
    if (index == 3) {
      // wx.showToast({
      //   title: '暂未开放',
      //   icon: 'error'
      // })
      wx.navigateTo({
        url: '/pages/course/course',
      })
    }
  },
  majorInfo(e) {
    // console.log(e);
    let id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/majorinfo/majorinfo?id=' + id,
    })
  },
  schoolInfo(e) {
    // console.log(e);
    let id = e.currentTarget.id;
    wx.navigateTo({
      url: '/pages/schoolinfo/schoolinfo?id=' + id,
    })
  },
  majorList(e) {
    wx.navigateTo({
      url: '/pages/major/major',
    })
  },
  schoolList(e) {
    wx.navigateTo({
      url: '/pages/school/school',
    })
  },
  newsList(e) {
    wx.navigateTo({
      url: '/pages/newslists/newslists',
    })
  },
})